package com.selfdriving.activity;

import java.io.File;
import java.io.FileNotFoundException;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.bean.MyFile;
import com.selfdriving.utils.BitmapUtil;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.WriteCamera;

/** 通行证申述 */
public class ActivityVerifyState extends BaseFragmentActivity implements
		OnClickListener
{
	private ImageView goBreak;
	private TextView topText;
	EditText editText1;
	EditText editText2;
	Button button1;
	Button button2;
	ImageButton addImg;
	EditText content;
	String imgUri;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_verify_state);
		initViews();
		initEvents();
	}

	@Override
	protected void initViews()
	{
		// TODO Auto-generated method stub
		content = (EditText) findViewById(R.id.verify_content);
		addImg = (ImageButton) findViewById(R.id.verify_addImg);
		button1 = (Button) findViewById(R.id.btn_back);
		button2 = (Button) findViewById(R.id.btn_ok);
		editText1 = (EditText) findViewById(R.id.verify_shenfenzheng);
		editText2 = (EditText) findViewById(R.id.verify_carCode);
		goBreak = (ImageView) findViewById(R.id.head_break);
		topText = (TextView) findViewById(R.id.top_text_middle);
		topText.setText("通行证申述");
		goBreak.setVisibility(View.VISIBLE);
	}

	@Override
	protected void initEvents()
	{
		// TODO Auto-generated method stub
		goBreak.setOnClickListener(this);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		addImg.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.head_break:
			finish();
			break;
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_ok:
			try
			{
				Post();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			break;
		case R.id.verify_addImg:
			new WriteCamera(ActivityVerifyState.this, ActivityVerifyState.this);
			break;
		}
	}

	public String getRealPathFromURI(Uri contentUri)
	{
		String res = null;
		if (contentUri != null)
		{
			String[] proj = { MediaStore.Images.Media.DATA };
			Cursor cursor = getContentResolver().query(contentUri, proj, null,
					null, null);
			if (cursor != null)
			{
				if (cursor.moveToFirst())
				{
					int column_index = cursor
							.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					res = cursor.getString(column_index);
				}
				cursor.close();
			}
		}
		return res;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		try
		{

			if (resultCode != Activity.RESULT_OK)
			{
				return;
			}
			switch (requestCode)
			{
			case Constant.PICK_FROM_FILE:// 取得本地图片
				Constant.imageCaptureUri = data.getData();
				Constant.fileRealPath = getRealPathFromURI(Constant.imageCaptureUri);
				imgUri = getRealPathFromURI(Constant.imageCaptureUri);
				Bitmap bitmap = BitmapUtil
						.getSamplingImage(Constant.fileRealPath);
				if (bitmap != null)
				{
					addImg.setImageBitmap(bitmap);
				}
				break;
			case Constant.PICK_FROM_CAMERA:// 拍照
				Constant.fileRealPath = Constant.imageCaptureUri.getPath();
				imgUri = Constant.imageCaptureUri.getPath();
				bitmap = BitmapUtil.getSamplingImage(Constant.fileRealPath);
				if (bitmap != null)
				{
					addImg.setImageBitmap(bitmap);
				}
				break;
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void Post()
	{
		// String idCard = editText1.getText().toString();
		// String carCards = editText2.getText().toString();
		String con = content.getText().toString();
		if (con.length() < 1)
		{
			showShortToast("请输入申述内容");
			return;
		}
		if (imgUri == null || imgUri.equals(""))
		{
			showShortToast("请输入上传您的行驶证照片");
			return;
		}
		MyFile myFile = new MyFile("img", new File(imgUri));
		AjaxParams params = new AjaxParams();
		params.put("f", "80");
		params.put("phone", mApplication.user.getPhone());
		params.put("content", con);
		try
		{
			params.put(myFile.getFileName(), myFile.getFile());
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		showLoadingDialog("通行证申述中...");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				showShortToast("服务器连接失败");
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				if (json.contains("999999"))
				{
					showShortToast("很抱歉，该车牌号已被验证，如非您本人请进行申述");
				}
				if (json.contains("000000"))
				{
					showShortToast("验证成功");
					finish();
				} else
				{
					showShortToast("验证失败");
				}
				dismissLoadingDialog();
			}
		});
	}
}
